<?php

namespace App\Http\Controllers\SystemManagement;

use App\Http\Controllers\IndexController;
use App\Models\SystemManagement\Post;
use Illuminate\Http\Request;

/**
 * Class PostController
 * @package App\Http\Controllers\SystemManagement
 * @group 岗位管理
 */
class PostController extends IndexController
{
    /**
     * 新增岗位
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     *
     * @bodyParam name string required 岗位名称
     * @bodyParam organization_id integer required 组织ID 外键 组织表
     * @bodyParam department_id integer required 部门ID 外键 部门表
     * @bodyParam description text required 岗位描述
     * @bodyParam number integer required 岗位人数
     * @bodyParam data_state boolean required 数据状态 0未审核 1已审核
     * @bodyParam disabled_state boolean required 禁用状态 0反禁用-启用 1禁用
     *
     * @response 201 {"code":201,"msg":"created","data":"成功"}
     * @response 400 {"code":400,"msg":"failed","data":"失败"}
     */
    public function createPost(\App\Http\Requests\System\Post $request){
        return $this->create($request,new Post());
    }

    /**
     * 岗位列表--带分页
     * @return \Illuminate\Http\JsonResponse
     *
     * @bodyParam organization_id 组织ID
     * @bodyParam department_id 部门ID
     *
     * @queryParam id 岗位id
     * @queryParam name 岗位名称
     * @queryParam organization_id 组织ID
     * @queryParam organization->name 组织名称
     * @queryParam department_id 部门ID
     * @queryParam department->name 部门名称
     * @queryParam description 岗位描述
     * @queryParam number 岗位人数
     * @queryParam data_state 数据状态 0未审核 1已审核
     * @queryParam disabled_state 禁用状态 0反禁用-启用 1禁用
     *
     */
    public function getPost(Request $request){
        return $this->getList(new Post(),$request);
    }

    /**
     * 岗位列表--不带分页
     * @return \Illuminate\Http\JsonResponse
     *
     * @bodyParam organization_id 组织ID
     * @bodyParam department_id 部门ID
     *
     * @queryParam id 岗位id
     * @queryParam name 岗位名称
     * @queryParam organization_id 组织ID
     * @queryParam organization->name 组织名称
     * @queryParam department_id 部门ID
     * @queryParam department->name 部门名称
     * @queryParam description 岗位描述
     * @queryParam number 岗位人数
     * @queryParam data_state 数据状态 0未审核 1已审核
     * @queryParam disabled_state 禁用状态 0反禁用-启用 1禁用
     *
     * @response 200 {"code":200,"msg":"success","data":"成功"}
     * @response 404 {"code":404,"msg":"not found","data":"没有添加岗位"}
     */
    public function getAll(Request $request){
        $post = Post::orderBy('id','desc')
                ->with(['organization','department'])
                ->where(function($query) use ($request){
                    $organization_id = $request->input('organization_id','');
                    $department_id = $request->input('department_id');
                    if(!empty($organization_id)) {
                        $query->where('organization_id',$organization_id);
                    }
                    if(!empty($department_id)) {
                        $query->where('department_id',$department_id);
                    }
                })
                ->get();
        if($post->isEmpty()){
            return response()->json(encodeMsg("没有添加岗位",404));
        }
        return response()->json(encodeMsg($post,200));
    }


    /**
     * 查看岗位
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     *
     * @queryParam id 岗位id
     * @queryParam name 岗位名称
     * @queryParam organization_id 组织ID
     * @queryParam organization->name 组织名称
     * @queryParam department_id 部门ID
     * @queryParam department->name 部门名称
     * @queryParam description 岗位描述
     * @queryParam number 岗位人数
     * @queryParam data_state 数据状态 0未审核 1已审核
     * @queryParam disabled_state 禁用状态 0反禁用-启用 1禁用
     *
     * @response 200 {"code":200,"msg":"Success","data":{"id":1,"mark":"1","name":"\u521d\u7ea7\u73af\u536b\u5de5","full_name":"\u73af\u536b\u5de5","is_leader":1,"department_id":1,"description":"\u521d\u7ea7\u73af\u536b\u5de5","create_organ_id":1,"use_organ_id":1,"memo_code":null,"creator":null,"create_time":null,"disable":null,"disable_time":null,"disable_state":0,"data_state":1}}
     */
    public function getOnePost($id){
        return $this->show($id,new Post());
    }

    /**
     * 修改岗位
     * @param Request $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     *
     * @bodyParam name string required 岗位名称
     * @bodyParam organization_id integer required 组织ID 外键 组织表
     * @bodyParam department_id integer required 部门ID 外键 部门表
     * @bodyParam description text required 岗位描述
     * @bodyParam number integer required 岗位人数
     * @bodyParam data_state boolean required 数据状态 0未审核 1已审核
     * @bodyParam disabled_state boolean required 禁用状态 0反禁用-启用 1禁用
     *
     * @response 205 {"code":205,"msg":"updated","data":"成功"}
     * @response 400 {"code":400,"msg":"failed","data":"失败"}
     */
    public function updatePost(\App\Http\Requests\System\Post $request,$id){
        return $this->update($request,$id,new Post());
    }

    /**
     * 批量删除岗位
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     *
     * @bodyParam ids array 岗位ID,批量删除+单条删除,以数组形式
     *
     * @response  204 {"code":204,"msg":"deleted","data":"删除成功"}
     * @response  400 {"code":400,"msg":"failed","data":"删除失败/数据格式错误"}
     */
    public function del(Request $request){
        return $this->delete($request,new Post());
    }

    /**
     * 批量恢复岗位
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @bodyParam ids array 岗位ID,批量恢复+单条恢复,以数组形式
     *
     * @response  204 {"code":204,"msg":"deleted","data":"恢复成功"}
     * @response  400 {"code":400,"msg":"failed","data":"恢复失败/数据格式错误"}
     */
    public function rec(Request $request){
        return $this->recover($request,new Post());
    }

    /**
     * 搜索岗位
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     *
     * @bodyParam name string required 岗位名
     *
     * @queryParam id 岗位id
     * @queryParam name 岗位名称
     * @queryParam organization_id 组织ID
     * @queryParam organization->name 组织名称
     * @queryParam department_id 部门ID
     * @queryParam department->name 部门名称
     * @queryParam description 岗位描述
     * @queryParam number 岗位人数
     * @queryParam data_state 数据状态 0未审核 1已审核
     * @queryParam disabled_state 禁用状态 0反禁用-启用 1禁用
     *
     * @response 200 {"code":200,"msg":"success","data":"成功"}
     * @response 400 {"code":400,"msg":"param error","data":"参数错误"}
     * @response 404 {"code":404,"msg":"not found","data":"未找到"}
     */
    public function getBy(Request $request){
        // $condition = $request->input('name');
        return $this->getByCond($request,new Post());
    }
}
